package com.ymatou.iapi.selleraccount.testcase;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.selleraccount.service.CreateRoleCall;
import com.ymatou.iapi.selleraccount.service.DeleteRoleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmlCreateRole;
import com.ymttest.database.sqlwapper.YmlCreateRoleIMapper;

/**
 * @serial 2016-12-26
 * @author chencan
 * 子账号的角色创建
 */
public class Tc_CreateRole {
	private static CreateRoleCall createRoleCall;
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("业务说明：子账号角色创建");
	}

	@Before
	public void caseUp() {
		createRoleCall = new CreateRoleCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(createRoleCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateRole_001() {
		Logger.start(true, "子账号角色创建");
		try {
			
			String roleName ="测试007";
			
			List<YmlCreateRole> account = new YmlCreateRoleIMapper().selectCreateRoleSellerId(roleName);
			YmlCreateRole first =account.get(0);
			int roleid = first.getRoleId();
			int sellerid = first.getSellerID();
			
			Map<String,Object> map1 = new HashMap<String,Object>();
			map1.put("Roleid",roleid);
			map1.put("SellerId",sellerid);
			
			JSONObject retBeas = new DeleteRoleCall().callAndGetReturnData(map1);
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("RoleName",roleName);
			
			JSONObject retBea = new CreateRoleCall().callAndGetReturnData(map);
			JSONObject data = retBea.getJSONObject("Data");
			int code = retBea.getInt("Code");
			Boolean success = data.getBoolean("Success");
			String message = data.getString("ErrorMessage");
			
			List<YmlCreateRole> accounts = new YmlCreateRoleIMapper().selectCreateRoleSellerId(roleName);
			YmlCreateRole firsts =accounts.get(0);
			int deleted = firsts.getIsDeleted();
			String name = firsts.getRoleName();
			int seller = firsts.getSellerID();
			
			Logger.verifyEquals(200, code, "code返回结果");
			Logger.verifyEquals(true, success, "返回结果状态");
			Logger.verifyEquals("创建角色成功", message, "描述");
			Logger.verifyEquals(1, deleted, "角色状态");
			Logger.verifyEquals(roleName, name, "角色名称");
			Logger.verifyEquals(sellerid, seller, "买手id");

			
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateRole_002() {
		Logger.start(true, "子账号角色创建 -- 名称重复");
		try {
			int sellerID =3383;
			List<YmlCreateRole> accounts = new YmlCreateRoleIMapper().selectCreateRoleIsdeleted(sellerID);
			YmlCreateRole firsts =accounts.get(0);
			String roleName = firsts.getRoleName();
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("RoleName",roleName);
			map.put("SellerId",sellerID);
			
			JSONObject retBea = new CreateRoleCall().callAndGetReturnData(map);
			JSONObject data = retBea.getJSONObject("Data");
			
			int code = retBea.getInt("Code");
			Boolean success = data.getBoolean("Success");
			String message = data.getString("ErrorMessage");
			
			
			Logger.verifyEquals(200, code, "code返回结果");
			Logger.verifyEquals(false, success, "返回结果状态");
			Logger.verifyEquals("名称重复不能保存", message, "错误描述");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_CreateRole_003() {
		Logger.start(true, "子账号角色创建 -- 菜单不存在");
		try {
			String menuidList ="0";
			String rolename = "测试1001";
			
			Map<String,Object> map = new HashMap<String,Object>();
			map.put("MenuidList",menuidList);
			map.put("RoleName",rolename);
			
			JSONObject retBea = new CreateRoleCall().callAndGetReturnData(map);
			JSONObject data = retBea.getJSONObject("Data");
			
			int code = retBea.getInt("Code");
			Boolean success = data.getBoolean("Success");
			String message = data.getString("ErrorMessage");
			
			
			
			Logger.verifyEquals(200, code, "code返回结果");
			Logger.verifyEquals(false, success, "返回结果状态");
			Logger.verifyEquals("新建角色失败", message, "错误描述");
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
